package com.sh.ssao.lottery.choujiang.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.ssao.lottery.choujiang.bean.BachInfo;
import com.sh.ssao.lottery.choujiang.bean.LuckDraw;
import com.sh.ssao.lottery.choujiang.mapper.BachInfoMapper;
import com.sh.ssao.lottery.choujiang.mapper.LuckDrawMapper;
import com.sh.ssao.lottery.choujiang.service.ILuckDrawService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sh.ssao.lottery.config.utils.GeneratorUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.text.StyledEditorKit;
import java.util.*;

/**
 * <p>
 * 中奖号码库 服务实现类
 * </p>
 *
 * @author gzgs
 * @since 2022-12-08 15:27:40
 */
@Service
public class LuckDrawServiceImpl extends ServiceImpl<LuckDrawMapper, LuckDraw> implements ILuckDrawService {

        @Autowired
        LuckDrawMapper mapper;
        @Autowired
        BachInfoMapper bachInfoMapper;
        /**
         * 中奖号码库分页查询数据
         * @param page
         * @param queryWrapper
         */
        @Override
        public IPage< LuckDraw> listPage(Page page, QueryWrapper queryWrapper){
             return mapper.listPage(page,queryWrapper);
        }

        /**
         * 中奖号码库导出数据
         * @param queryWrapper
         */
        @Override
        public List<Map<String, Object>> export(QueryWrapper queryWrapper){
            return mapper.export(queryWrapper);
        }

        /**
         * 中奖号码库保存数据
         * @param bean
         */
        @Transactional
        @Override
        public void saveMore( LuckDraw bean){
            //查询配置数据
            List<BachInfo> bachInfos = bachInfoMapper.selectList(new QueryWrapper<>());
            int min = 0;//随机最小值
            int max = 0;//随机最大值
            int total = 0;//随机个数
            for (BachInfo bachInfo : bachInfos) {
                if (null!=bachInfo.getBachId()){
                    if (1==bachInfo.getBachId()){
                        min = bachInfo.getBackNo();
                    }
                    if (2==bachInfo.getBachId()){
                        max = bachInfo.getBackNo();
                    }
                    if (3==bachInfo.getBachId()){
                        total = bachInfo.getBackNo();
                    }
                }
            }
            log.debug("随机最小值:"+min);
            log.debug("随机最大值:"+max);
            log.debug("随机个数:"+total);
            int i=1;
            Set<Integer> luckNum = GeneratorUtil.getLuckNum(min, max, total);
            for (Integer integer : luckNum) {
                LuckDraw luckDraw = mapper.selectById(i);
                if (null != luckDraw && null!=luckDraw.getLdId() && !"".equals(luckDraw.getLdId())){
                    LuckDraw entity = new LuckDraw();
                    UpdateWrapper<LuckDraw> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.set("ld_no",integer);//设置签号
                    updateWrapper.set("creat_time",new Date());
                    updateWrapper.eq("ld_id",i);
//                    updateWrapper.eq("user_full","");
//                    updateWrapper.eq("is_car","");

                    int update = mapper.update(entity, updateWrapper);
                    log.debug("跟新数据"+i+"成功："+integer);
                }else {
                    LuckDraw entity = new LuckDraw();
                    entity.setLdId(i);//设置ID
                    entity.setLdNo(integer);//设置签号
                    entity.setCreatTime(new Date());
                    mapper.insert(entity);
                }
                i++;//计数器
            }
        }

        /**
         * 中奖号码库修改数据
         * @param bean
         */
        @Transactional
        @Override
        public void updateMore( LuckDraw bean){
            mapper.updateById(bean);
        }

        /**
         * 中奖号码库删除数据
         * @param bean
         */
        @Override
        public void delMore( LuckDraw bean){
             QueryWrapper< LuckDraw> queryWrapper=new QueryWrapper<>();
             queryWrapper.eq("ld_id",bean.getLdId());
                    mapper.delete(queryWrapper);
        }

}
